import { defineStore, storeToRefs } from 'pinia'
import { useFormStore } from '@/stores/formData.js'
import { useMultipleUIStore } from '@/stores/multipleTaskUI.js'

const multipleUIStore = useMultipleUIStore()
const { excelDisabled, drawingBtnDisabled, loading } = storeToRefs(multipleUIStore)

export const useMultipleTaskData = defineStore('multipleTaskData', () => {
  //
  const tableValues = reactive({})
  // 表的第一列数据（问题名称）
  const tableRows = ref([])
  // 定义表头（算法名字）
  const tableHead = ref([])
  // 选择的指标值
  const indicator = ref('num of run')
  // 算法指标下拉框
  const indicators = ref({})
  // 保存统计结果
  const multi_statistical_result = reactive({})
  // 保存最优值索引
  const best_value_index = reactive({})
  const columnsLength = ref(0)
  const rowsLength = ref(0)
  // 保存用于生成图标的problem数据
  const problemList = ref([])
  // 保存当前选择的problem
  const selectedProblems = ref([])
  // 保存用于生成图标的algorithm数据
  const algorithmList = ref([])
  // 保存当前选择的algorithm数据
  const selectedAlgorithms = ref([])
  // 保存所有的统计类型
  const statisticalOptions = ref(['ranksums', 'signrank'])
  // 保存当前选择的统计类型
  const statisticalOption = ref('ranksums')
  // 是否保存图标为pdf文件
  const savePdf = ref(false)

  function create2DArray(rows, columns, fillValue = 0) {
    const arr = new Array(rows)
    for (let i = 0; i < rows; i++) {
      arr[i] = new Array(columns).fill(fillValue)
    }
    return arr
  }

  function clearAllData() {
    tableRows.value = []
    tableHead.value = []
    problemList.value = []
    algorithmList.value = []
    selectedProblems.value = []
    selectedAlgorithms.value = []
    if (loading.value != null) {
      loading.value.close()
    }
    // 清空对象
    clearObjProperties(tableValues, multi_statistical_result, best_value_index)
  }

  const clearObjProperties = (...objs) => {
    objs.forEach(obj => {
      Object.keys(obj).forEach(key => {
        delete obj[key]
      })
    })
  }

  const initMultipleTableData = (tableData) => {
    columnsLength.value = tableData.uploadAlgorithmsData.length
    rowsLength.value = tableData.uploadProblemData.length
    for (let i = 0; i < columnsLength.value; i++) {
      tableHead.value.push(tableData.uploadAlgorithmsData[i].name)
      algorithmList.value.push({ key: i, label: tableData.uploadAlgorithmsData[i].name, disabled: false })
    }
    let index = 0
    for (const problem of tableData.uploadProblemData) {
      tableRows.value.push(problem.name)
      problemList.value.push({ key: index, label: problem.name, disabled: false })
      index++
    }
    tableValues[indicator.value] = create2DArray(rowsLength.value, columnsLength.value)
    initStatisticalResult()
  }
  // 更新表格显示的数据
  const updateMultipleTable = (message) => {
    const { indicator_name, indicator_value, problem_index, algorithm_index } = message
    const temp = tableValues[indicator.value] || create2DArray(tableRows.value.length, tableHead.value.length)
    if (indicator_name == 'num of run') {
      temp[problem_index][algorithm_index] = indicator_value + temp[problem_index][algorithm_index]
    } else {
      temp[problem_index][algorithm_index] = indicator_value
    }
    tableValues[indicator.value] = temp
  }

  const statistical_result = (message) => {
    multi_statistical_result[indicator.value][statisticalOption.value] = message['statistical_result']
    best_value_index[indicator.value] = message['best_value_index']
  }

  const initStatisticalResult = () => {
    multi_statistical_result[indicator.value] = multi_statistical_result[indicator.value] || {}
    multi_statistical_result[indicator.value][statisticalOption.value] = create2DArray(rowsLength.value + 1, columnsLength.value - 1, '')
    best_value_index[indicator.value] = create2DArray(rowsLength.value, columnsLength.value, false)
  }



  return {
    tableValues,
    tableRows,
    tableHead,
    initMultipleTableData,
    clearAllData,
    indicator,
    indicators,
    updateMultipleTable,
    statistical_result,
    multi_statistical_result,
    initStatisticalResult,
    statisticalOptions,
    statisticalOption,
    best_value_index,
    selectedProblems,
    selectedAlgorithms,
    algorithmList,
    problemList,
    savePdf,
    rowsLength
  }
})
